//
//  HtmlViewController.swift
//  lottery
//
//  Created by admin on 16/2/4.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class HtmlViewController: UIViewController,UIWebViewDelegate {
    weak var help:Help!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = self.help.title
        let webview = UIWebView(frame: UIScreen.mainScreen().bounds)
        webview.delegate = self
        // 获得html文件路径
        let path = NSBundle.mainBundle().pathForResource("help.html", ofType: nil)
        // 获得请求字串
        let request = NSURLRequest(URL: NSURL(fileURLWithPath: path!))
        // 使用webView加载html
        webview.loadRequest(request)
        // 因为是使用模态框加载的这个控制器，所以需要手动设置下返回按钮
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "back")
        // 把webView添加进视图
        self.view.addSubview(webview)
    }
    // 返回之前的控制器
    func back()
    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    // webView代理方法，加载完成时调用
    func webViewDidFinishLoad(webView: UIWebView) {
        // 执行js代码
        let jsCode = "window.location.href='#\(self.help.id)'"
        webView.stringByEvaluatingJavaScriptFromString(jsCode)
    }
}
